include constants.doh

// See the README for instructions on how to access this data
import delimited "exposure_groups_regressions.csv", clear

// Make the age variables
gen age_bracket_18_34 = age_bracket == "18-24" | age_bracket == "25-34"
gen age_bracket_35_54 = age_bracket == "35-44" | age_bracket == "45-54"
gen age_bracket_55 = age_bracket == "55-64" | age_bracket == "65+"

// Make clean buckets
encode age_bracket, gen(age_bracket_clean)

// Make log RHS
gen log_fwc_jh = log(frnd_weighted_cases_jh_early)
gen log_fwc_jh_jun = log(frnd_weighted_cases_jh_june)

// iPhone can be NULL in rare cases
// This checks if it got converted to string
local vartype: type has_iphone
if substr("`vartype'",1,3)=="str" {
    gen has_iphone_temp = has_iphone
    drop has_iphone
    gen has_iphone = 0
    replace has_iphone = 1 if has_iphone_temp == "1"
}

// Make the zcta/county terciles
gen top_tercile_zcta_inc = zcta_median_hh_inc_tercile == 1
gen mid_tercile_zcta_inc = zcta_median_hh_inc_tercile == 2
gen bot_tercile_zcta_inc = zcta_median_hh_inc_tercile == 3

gen top_tercile_case100k = county_cases_per_100k_jh_tercile == 1
gen mid_tercile_case100k = county_cases_per_100k_jh_tercile == 2
gen bot_tercile_case100k = county_cases_per_100k_jh_tercile == 3

// Make friend weighted measures
egen fw_median_hh_inc_grp = cut(frnd_weighted_median_hh_inc), group(100)
egen fw_pop_density_grp = cut(frnd_weighted_pop_density), group(100)
egen fw_frac_urban_grp = cut(frnd_weighted_frac_urban), group(100)

// Make interaction group
fegen interaction_group = group(home_zcta is_female has_college age_bracket_clean has_iphone has_tablet)

// Make percentiles of groups from pre-period
egen grps_from_pre_ptile = cut(groups_from_preperiod), group(100)

compress


///// Generate Pieces of Table 4 and Table A17 //////

eststo clear

eststo panel1: reghdfe in_lockdown_group log_fwc_jh, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe in_lockdown_group log_fwc_jh_jun, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe in_lockdown_group log_fwc_jh age_bracket_35_54 age_bracket_55 is_female has_college has_iphone has_tablet mid_tercile_zcta_inc top_tercile_zcta_inc mid_tercile_case100k top_tercile_case100k, poolsize(5) absorb(fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

xml_tab panel*, save("output/tables/covac_groups_constant_baseline") stats(N r2 ymean) replace below

eststo clear


///// Robustness - Alternative Exposure Dates, Table A19 //////

eststo clear

eststo panel1: reghdfe in_lockdown_group log_fwc_100k_early, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe in_lockdown_group log_fwc_100k_mar, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe in_lockdown_group log_fwc_100k_apr, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe in_lockdown_group log_fwc_100k_may, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe in_lockdown_group log_fwc_100k_jun, poolsize(5) absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

xml_tab panel*, save("output/tables/covac_groups_constant_alt_exposure_dates") stats(N r2 ymean) replace below

eststo clear


////// Pieces of Table A20, Table A21, and Table A22 //////

// Make all the new necessary vars
gen log_fwcXage_18_34 = log_fwc_jh*age_bracket_18_34
gen log_fwcXage_35_54 = log_fwc_jh*age_bracket_35_54
gen log_fwcXage_55 = log_fwc_jh*age_bracket_55

gen log_fwcXfemale = log_fwc_jh*(is_female==1)
gen log_fwcXmale = log_fwc_jh*(is_female==0)

gen log_fwcXcollege = log_fwc_jh*(has_college==1)
gen log_fwcXno_college = log_fwc_jh*(has_college==0)

gen log_fwcXbot_inc = log_fwc_jh*bot_tercile_zcta_inc
gen log_fwcXmid_inc = log_fwc_jh*mid_tercile_zcta_inc
gen log_fwcXtop_inc = log_fwc_jh*top_tercile_zcta_inc

gen log_fwcXbot_case100k = log_fwc_jh*bot_tercile_case100k
gen log_fwcXmid_case100k = log_fwc_jh*mid_tercile_case100k
gen log_fwcXtop_case100k = log_fwc_jh*top_tercile_case100k

gen log_fwc_jh_1_25 = log(friends_1_25_weighted_cases_jh_e)
gen log_fwc_jh_26_50 = log(friends_26_50_weighted_cases_jh_)
gen log_fwc_jh_51_75 = log(friends_51_75_weighted_cases_jh_)
gen log_fwc_jh_76_100 = log(friends_76_100_weighted_cases_jh)

eststo clear

eststo panel1: reghdfe in_lockdown_group log_fwcXage_18_34 log_fwcXage_35_54 log_fwcXage_55, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe in_lockdown_group log_fwcXfemale log_fwcXmale, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe in_lockdown_group log_fwcXcollege log_fwcXno_college, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe in_lockdown_group log_fwcXbot_inc log_fwcXmid_inc log_fwcXtop_inc, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe in_lockdown_group log_fwcXbot_case100k log_fwcXmid_case100k log_fwcXtop_case100k, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel6: reghdfe in_lockdown_group log_fwc_jh_1_25 log_fwc_jh_26_50 log_fwc_jh_51_75 log_fwc_jh_76_100, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp grps_from_pre_ptile) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm
test log_fwc_jh_1_25 = log_fwc_jh_76_100
estadd scalar F_top_bot = r(F)
estadd scalar p_top_bot = r(p)

xml_tab panel*, save("output/tables/covac_groups_heteros_in_lockdown_group") stats(N r2 ymean F_top_bot p_top_bot) replace below

eststo clear
